package view.components;

import java.awt.Color;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JLabel;
import javax.swing.Timer;

import org.jdesktop.swingx.JXStatusBar;

import control.StatusMessageHandler;


/**
 * StatusBar welche Nachrichten des MessageHandlers entgegennimmt
 */
@SuppressWarnings("serial")
public class ExtendedJXStatusBar extends JXStatusBar implements Observer {
	private JLabel statusLabel = new JLabel(" ");
	private Timer timer;
	
	public ExtendedJXStatusBar(){
		add(statusLabel);
		StatusMessageHandler.getInstance().addObserver(this);
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		IconizedInterface msg = StatusMessageHandler.getMessage();
		if(msg != null){
			displayMsg(msg);
		}
	}

	private void displayMsg(IconizedInterface msg) {
		if(timer != null && timer.isRunning()){
			timer.stop();
		}
		
		statusLabel.setForeground(SystemColor.textText);
		statusLabel.setText(msg.toString());
		statusLabel.setIcon(msg.getIcon());
		
		timer = new Timer(500, new TimerActionListener());
		timer.start();
	}

	private class TimerActionListener implements ActionListener{
		Color from = SystemColor.textText;
		Color to = SystemColor.textInactiveText;
		int counter = 10;
		int deltaR = (to.getRed() - from.getRed())/counter;
		int deltaG = (to.getGreen() - from.getGreen())/counter;
		int deltaB = (to.getBlue() - from.getBlue())/counter;
		
		@Override
		public void actionPerformed(ActionEvent e) {
			if(counter-- > 0){
				from = new Color(from.getRed() + deltaR, from.getGreen() + deltaG, from.getBlue() + deltaB);
				statusLabel.setForeground(from);
				//statusLabel.setForeground(SystemColor.textInactiveText);
			} else {
				((Timer)e.getSource()).stop();
			}
		}
	}
}
